package com.gxa.admin.recycling.controller;
import com.gxa.admin.recycling.service.OutOrderService;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.OutOrder;
import com.gxa.core.recycling.pojo.OutOrderItem;
import com.gxa.core.recycling.pojo.OutOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/outOrder")
@Api(tags = "物资出库管理")
public class OutOrderController {
    @Autowired
    OutOrderService outOrderService = null;

    private List<OutOrderItem> list = new ArrayList<>();

    @PostMapping("/findAll")
    @ApiOperation("查询所有出库订单")
    public Map findAll(Integer page, Integer size, Long id, String state, String time) {
        Map<Integer, List<Map>> map = outOrderService.findAllOutOrder(id, state, time, page, size);
        Set<Integer> integers = map.keySet();
        JsonResult jsonResult = new JsonResult();
        for (Integer integer : integers) {
            jsonResult.setCode("200");
            jsonResult.setData(map.get(integer));
            jsonResult.put("total", integer);
        }
        return jsonResult.getDataMap();

    }

    @PostMapping("/findOutOrderDetailById")
    @ApiOperation("根据id查询出库订单详情")
    public Map findOutOrderById(@RequestParam("id") Long id) {

        OutOrderVo outOrderDetailById = outOrderService.findOutOrderDetailById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(outOrderDetailById);
        return jsonResult.getDataMap();

    }
    @PostMapping("/addOutOrderItem")
    @ApiOperation("新加出库单项")
    public Map addOutOrderItem(@RequestParam("wasteId") Long wasteId, @RequestParam("outNumber") Integer outNumber
            , @RequestParam("price") Double price, @RequestParam("total") Double total, @ApiIgnore HttpSession session) {

        OutOrderItem outOrderItem = new OutOrderItem();
        outOrderItem.setWasteId(wasteId);
        outOrderItem.setOutNumber(outNumber);
        outOrderItem.setPrice(price);
        outOrderItem.setTotal(total);
        System.out.println(outOrderItem);
        list.add(outOrderItem);
        session.setAttribute("list", list);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        //添加成功后删除
        return jsonResult.getDataMap();
    }

    @PostMapping("/addOutOrder")
    @ApiOperation("新增出库单")
    public Map addOutOrder(@RequestBody OutOrder outOrder, @ApiIgnore HttpSession session) throws BusinessException {


        List<OutOrderItem> list = (List<OutOrderItem>) session.getAttribute("list");

        outOrderService.addOutOrder(outOrder, list);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        //添加成功后删除
        session.removeAttribute("list");
        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("批审")
    public Map update(@RequestParam("id") Long id, @RequestParam("state") String state,@RequestParam("userId") Long userId) throws BusinessException {

        OutOrder outOrder = new OutOrder();
        outOrder.setId(id);
        outOrder.setState(state);
        outOrder.setAuditManagerId(userId);
        outOrderService.approval(outOrder);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("删除出库单")
    public Map delete(@RequestParam("id") Long id) {
        outOrderService.deleteOutOrder(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/reset")
    @ApiOperation("重置出库单项")
    public void reset(@ApiIgnore HttpSession session) {
        session.removeAttribute("list");
    }
}
